﻿/// <reference path="../ext-3.3.1/adapter/ext/ext-base.js" />
/// <reference path="../ext-3.3.1/ext-all.js" />

Ext.namespace("Login");

Login.Application = {
    logoPanel: function () {
        return new Ext.Panel({
            baseCls: 'x-plain',
            id: 'login-logo',
            region: 'center',
            html: '<h3><center>这里是LOGO！</center></h3>'
        });
    },
    loginForm: function () {
        return new Ext.form.FormPanel({
            id: 'login-form',
            region: 'south',
            baseCls: 'x-plain',
            labelWidth: 55,
            frame: true,
            bodyStyle: 'backgroud-color:#DFE8F6;padding-top:15px;padding-left:20px;border:0px solid;',
            defaults: {
                width: 230,
                anchor: '88%',
                allowBlank: false,
                selectOnFocus: true,
                msgTargt: 'side'
            },
            height: 80,
            defaultType: 'textfield',
            method: 'POST',
            bodyBorder: false,
            border: false,
            items: [{
                fieldLabel: '用户',
                id: 'txtName',
                name: 'username',
                regex: /^[\u4E00-\u9FA5A-Za-z0-9_]+$/,
                regexText: '只能输入汉字、字母、数字',
                blankText: '用户名不能为空！'
            }, {
                fieldLabel: '密码',
                name: 'password',
                inputType: 'password',
                blankText: '密码不能为空！'
            }]
        });
    },
    loginWindow: function () {
        var loginForm = this.loginForm();
        var doLogin = function () {
            if (loginForm.getForm().isValid()) {
                loginForm.form.submit({
                    waitMsg: '正在登录验证,请稍后...',
                    url: 'Service/UserLoginHandler.ashx',
                    success: function (form, action) {
                        if (action.result.success) {
                            document.location = "Index.aspx?departmentid=" + action.result.departmentid + "&userrole=" + action.result.userrole;
                        }
                        else {
                            Ext.Msg.alert("提示", action.result.message);
                        }

                    },
                    failure: function (from, action) {
                        Ext.Msg.alert("提示", action.result.message);
                    }
                });
            }
        };
        return new Ext.Window({
            title: "登录窗口",
            layout: 'border',
            el: 'login',
            width: 430,
            height: 210,
            plain: false,
            bodyStyle: 'padding:10px',
            maximizable: false,
            closable: false,
            collapsible: false,
            buttonAlign: 'center',
            keys: [{
                key: Ext.EventObject.ENTER,
                fn: doLogin
            }],
            items: [this.logoPanel(), loginForm],
            buttons: [{
                text: '登录',
                type: 'submit',
                handler: doLogin
            }, {
                text: '重置',
                handler: function () {
                    loginForm.form.reset();
                }
            }]
        });
    },
    onWindowResize: function (window) {
        Ext.EventManager.onWindowResize(function () {
            if (document.body.clientWidth > 800 && document.body.clientHeight > 400) {
                window.center();
            }

        });
    },
    init: function () {
        var loginWin = this.loginWindow();
        loginWin.show();
        this.onWindowResize(loginWin);
    }
}

Ext.onReady(function () {
    Ext.QuickTips.init();
    Login.Application.init();
});

